package com.fone.player.online.adapter;

import java.util.List;

import com.fone.player.R;
import com.fone.player.entity.SubjectInfo;
import com.fone.player.util.FoneUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubjectAdapter extends BaseAdapter {
	
	/** 日志标签 */
//	private static final String TAG = "CommonAdapter";

	private String shost;
//	private ArrayList<Cnt> cntList;
	private List<SubjectInfo> subjectList;
	private LayoutInflater layoutInflater;
	
	DisplayImageOptions options;

	public SubjectAdapter(Context context, String shost, List<SubjectInfo> subjectList) {
		super();
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_340_192)
		.showImageForEmptyUri(R.drawable.default_340_192)
		.showImageOnFail(R.drawable.default_340_192)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
		layoutInflater = LayoutInflater.from(context);
		initData(shost, subjectList);
	}

	public void initData(String shost, List<SubjectInfo> subjectList) {
		this.shost = shost;
		this.subjectList = subjectList;
	}

	@Override
	public int getCount() {
		return subjectList.size();
	}

	@Override
	public Object getItem(int position) {
		if(position<subjectList.size()){
			return subjectList.get(position);
		}else{
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SubjectInfo info = subjectList.get(position); 

		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_column, null);
			// 创建 ViewHodler 对象
			vHolder = new ViewHolder();
			vHolder.pic = (ImageView) convertView.findViewById(R.column.pic);
			vHolder.title = (TextView) convertView.findViewById(R.column.name);
			vHolder.time = (TextView) convertView.findViewById(R.column.time);
			// 设置 Tag
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}

		// 设置图片
//			String url =shost +info.pic1.substring(7, info.pic1.length());
//			ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("", shost, info.pic1), R.drawable.default_image_bg, vHolder.pic);
			ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic2), vHolder.pic, options);
			
//			L.v(TAG, "getView", url);
			vHolder.title.setText(info.name);
			vHolder.time.setText(info.desc.replace(" ", ""));
		
		return convertView;
	}

	static class ViewHolder {
		ImageView pic;
		TextView title;
		TextView time;
	}

	
}
